package com.shengde.jinwawaforleader.model.myJob.classList;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.TextView;

import com.shengde.jinwawaforleader.R;
import com.shengde.jinwawaforleader.base.BaseFragmentActivity;
import com.shengde.jinwawaforleader.base.BaseV4Fragment;
import com.shengde.jinwawaforleader.model.myJob.adapter.TabPageIndicatorAdapter;
import com.shengde.jinwawaforleader.model.myJob.fragment.AllClassFragment;
import com.shengde.jinwawaforleader.model.myJob.fragment.AllTeacherFragment;
import com.shengde.jinwawaforleader.network.BaseProtocol;
import com.shengde.jinwawaforleader.network.BasexUtilsProtocol;
import com.shengde.jinwawaforleader.network.protocolbean.ProClassList;
import com.shengde.jinwawaforleader.utils.MessageEvent;
import com.viewpagerindicator.TabPageIndicator;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;

import java.util.ArrayList;
import java.util.List;

/**
 * 班级列表
 * Created by Administrator on 2016/9/1 0001.
 */
public class ClassListActivity extends BaseFragmentActivity {
	private TextView tv_back,fill_add,tv_title;
	private TabPageIndicator indicator;
	private ViewPager pager;
	private ClassListActivity activity;
	public String[] TITLE;
	private ArrayList<BaseV4Fragment> list=new ArrayList<>();
	public List<ProClassList.StudentEntity> student;
	public List<ProClassList.TeacherEntity> teacher;
	private AllClassFragment classFragment;
	private AllTeacherFragment teacherFragment;

	@Override
	public void onRoot(Bundle savedInstanceState) {
		//绑定事件接受
		EventBus.getDefault().register(this);
		setContentView(R.layout.class_list_activity);
		activity = this;
	}

	@Override
	public void initView() {
		tv_back = (TextView) findViewById(R.id.tv_back);
		tv_title = (TextView)findViewById(R.id.tv_title);
		indicator = (TabPageIndicator)findViewById(R.id.indicator);
		pager = (ViewPager)findViewById(R.id.pager);
		fill_add = (TextView)findViewById(R.id.fill_add);
	}

	@Override
	public void initData() {
		tv_title.setText(getString(R.string.class_list));
		fill_add.setText(R.string.scan_add);
		TITLE = new String[] { getString(R.string.suoyoubanji), getString(R.string.suoyoulaoshi)};
		classFragment = new AllClassFragment();
		teacherFragment = new AllTeacherFragment();
		list.add(classFragment);
		list.add(teacherFragment);
		FragmentPagerAdapter adapter = new TabPageIndicatorAdapter(getSupportFragmentManager(),activity,list);
		pager.setAdapter(adapter);
		indicator.setViewPager(pager);//关联
		BaseProtocol.getInstance().postClassList(activity,classFragment,teacherFragment);
	}

	@Override
	public void initListener() {
		tv_back.setOnClickListener(this);
		fill_add.setOnClickListener(this);
	}

	@Override
	public void onWidgetClick(View view) {
		switch (view.getId()) {
			case R.id.tv_back:
				finish();
				break;
			case R.id.fill_add:
			    Intent intent = new Intent(this, AddClass.class);
			    startActivity(intent);
			    break;
		}
	}

	@Subscribe
	public void onMessageEvent(MessageEvent event){
		if(MessageEvent.TIAOBANCHENGGONG.equals(event.message)) {
			AllClassFragment classFragment = (AllClassFragment) list.get(0);
			AllTeacherFragment teacherFragment = (AllTeacherFragment) list.get(1);
			BaseProtocol.getInstance().postClassList(activity,classFragment,teacherFragment);
		}else if(MessageEvent.XIUGAIBANJIXINXI.equals(event.message)) {
			BasexUtilsProtocol.dialog.cancel();
			BaseProtocol.getInstance().postClassList(activity,classFragment,teacherFragment);
		}
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
		//注销事件接受
		EventBus.getDefault().unregister(this);
	}
}